<?php

class spFormComponents extends spDraggableClone
{
	private $prop = array(
		"text" => '
			{
				"Text Properties" : {
					"maxlength" : {
						"type" : "text",
						"value" : ""
					},	
					"size" : {
						"type" : "text",
						"value" : ""
					},
					"style" : {
						"type" : "textarea",
						"value" : ""
					}
				}
			}',
		"texts" => '
			{
				"Texts Properties" : {
				}
			}',
		"combobox" => '
			{
				"Combobox Properties" : {
					"list" : {
						"type" : "textarea"
					}
				}
			}',
		"date" => '
			{
				"Date Properties" : {
				}
			}',
		"email" => '
			{
				"Email Properties" : {
				}
			}'
	);

	public function  __construct( $data=null )
	{
		if ( $data==null )
			$data = array();
			
		$data["draggableContent"] = array( 
			$this->createComponent( $data, "text", 		$this->prop["text"],		"" ),
			$this->createComponent( $data, "texts", 	$this->prop["texts"], 		"" ),
			$this->createComponent( $data, "combobox", 	$this->prop["combobox"], 	"" ),
			$this->createComponent( $data, "date", 		$this->prop["date"], 		"" ),
			$this->createComponent( $data, "email", 	$this->prop["email"], 		"" )			
		);
		
		// this is where we want the propertysheet to be shown
		$data["addCols"] = array(
			new spDiv(array("class"=>"propertySheet2"))
		);
	
		parent::__construct( $data );
		parent::setClass( "spFormComponents" );
	}
	
	private function conformCompData( $compName, $compData )
	{
		// cleanup
		$prop = $this->prop[$compName];
		$prop = trim( $prop );
		$prop = stripslashes( $prop );
		$json = json_decode( $prop, true );

		$jsonData = json_decode( $compData, true );
		
		$merged = array_merge_recursive_distinct( $json, $jsonData );
		
		/*
		echo "<div style='border:solid red 2px;'>";
		var_dump( $json );
		var_dump( $jsonData );
		var_dump( $merged );
		echo "</div>";
		*/
		
		return json_encode( $merged );
	}

	/*	
		$data		: standard data used by spElements
		$name 		: name of component i.e. "text", "combobox"
		$compData	: json string of data
		$titleValue	: user supplied title. this is the title that comes in the forms fieldset
		$template 	: bool, if true then certain fields are hidden	
	*/	
	
	private function createComponent( $data, $name, $compData, $titleValue, $template=true )
	{
		if ( !isset($this->prop[$name]) )
			return false;
	
		// cleanup $compData
		//$value = preg_replace('/\s+/', '', $compData );
		$compData = trim( $compData );
		$compData = stripslashes( $compData );

		$attr = array();
		if ( $template==true )
		{
			$attr["disabled"] = "disabled";
		}
		
		// user title for component
		$compTitle = new spInput(array(
			"type"	=> "text",
			"name"	=> $data["name"]."title[]",
			"value"	=> $titleValue,
			"class"	=> "compContainerTitle",
			"style"	=> array(
				"display"=> $template==true?"none":"block",
				"float"=>"left"
			),
			"attr"	=> $attr
		));
		
		$closeButton = new spImage(array(
			"class" => "spDeleteContainer",
			"src" => get_option('home')."/wp-content/plugins/gym/sigmaphp/html/extended/close_button.png",
			"style"=>array(
				"display"=> $template==true?"none":"block",
				"float"=>"right"
			)
		));

		$compName = new spDiv(array(
			"content"=>$name,
			"class"=>"compContainerName",
			"style"=>array(
				"float"=>"left"
			)
		));				
		
		// we need to clean the $compData to make sure it conforms to the schema. This can happen when you change the schema but the database
		// has not updated.
		$compData = $this->conformCompData( $name, $compData );
	
		// data for component
		$compPropertyData = new spTextArea(array(
			"name"		=> $data["name"]."[]",
			"content" 	=> $compData,
			"class" 	=> "spPropertySheetDataInput",
			"attr"		=> $attr,
			"style"		=> array(
				"display" => "none"
			)
		));
		
		// store the component type here (the name is unqiue to the component type)
		$compCompType = new spInput(array(
			"type"	=>"hidden",
			"name"	=>$data["name"]."comptype[]",
			"value"	=>$name,
			"attr"	=> $attr
		));
	
		$container = new spDiv(array("class"=>"compContainer spPropertySheet"));
		$container->addChild( $compTitle );
		$container->addChild( $compName );
		$container->addChild( $compCompType );
		$container->addChild( $closeButton );
		$container->addChild( new spDiv(array("style"=>array("clear"=>"both"))) );
		$container->addChild( $compPropertyData );

		return $container;
	}
	
	public function setElementDataString( $s )
	{
		parent::setElementDataString( $s );	
		
		$ar = json_decode( $s, true );
		
		//var_dump( $ar );
		
		if ( count($ar) )
		{
			foreach ( $ar as $c )
			{
				// we need to insert the comp into the sortable list
				$comp = $this->createComponent( parent::getData(), $c["type"], json_encode($c["component"]), $c["title"], false );
				if ( $comp!=false )
					parent::add( $comp );
			}
		}
	}
	
	public function updateFromRequest()
	{
		parent::updateFromRequest();
		
		// convenience variables
		$data = parent::getData();
		$r = unMagicQuotify( $_REQUEST );

		if ( isset($r[$data["name"]]) )
		{
			$value = implode( "|", $r[$data["name"]] );
			
			$components 		= $r[$data["name"]];
			$componentTypes 	= $r[$data["name"]."comptype"];
			$componentTitles 	= $r[$data["name"]."title"];
			
			$json = array();
			
			for ( $i=0; $i<count($components); $i++ )
			{
				$json[] = array(
					"component" => json_decode($components[$i]),
					"type" 		=> $componentTypes[$i],
					"title" 	=> $componentTitles[$i]
				);
			}
			
			$s = json_encode( $json );
			$this->setElementDataString( $s );
		}
	}	
}

?>